package com.b1.controller;

import cn.hutool.core.bean.BeanUtil;
import com.b1.common.BaseResponse;
import com.b1.common.DeleteRequest;
import com.b1.common.ErrorCode;
import com.b1.common.ResultUtils;
import com.b1.exception.BusinessException;
import com.b1.exception.ThrowUtils;
import com.b1.pojo.dto.teacherSleepRecord.TeacherSleepRecorAddRequest;
import com.b1.pojo.dto.teacherSleepRecord.TeacherSleepRecordQueryRequest;
import com.b1.pojo.enity.TeacherSleepRecord;
import com.b1.service.TeacherSleepRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("b1/teacherSleepQuery")
@CrossOrigin
@Api(tags = "老师记录睡眠表")
@Slf4j
public class TeacherSleepRecordController {
    @Resource
    private TeacherSleepRecordService teacherSleepRecordService;
    @PostMapping("/add")
    @ApiOperation(value = "添加记录")
    public BaseResponse<Integer> addChildernSleepRecord(@RequestBody TeacherSleepRecorAddRequest teacherSleepRecorAddRequest){
        if (teacherSleepRecorAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TeacherSleepRecord teacherSleepRecord = new TeacherSleepRecord();

        BeanUtil.copyProperties(teacherSleepRecorAddRequest, teacherSleepRecord);
        //childernSleepRecordService.validChildernSleepRecord(childernSleepRecord, true);
        //时间由自己上传
        //teacherSleepRecord.setCheckTime(new Date());
        boolean result = teacherSleepRecordService.save(teacherSleepRecord);
        ThrowUtils.throwIf(!result ,ErrorCode.OPERATION_ERROR, "添加失败");
        Integer recordId = teacherSleepRecord.getId();
        return ResultUtils.success(recordId);
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除记录")
    public BaseResponse<Boolean> deleteChildernSleepRecord(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        TeacherSleepRecord byId = teacherSleepRecordService.getById(id);
        ThrowUtils.throwIf(byId == null, ErrorCode.NOT_FOUND_ERROR);
        boolean b = teacherSleepRecordService.removeById(id);
        return ResultUtils.success(b);
    }
    @PostMapping("/update")
    @ApiOperation(value = "更新午睡记录")
    public BaseResponse<Boolean> updatePost(@RequestBody  TeacherSleepRecord teacherSleepRecord) {
        if (teacherSleepRecord == null || teacherSleepRecord.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 参数校验
       // childernSleepRecordService.validChildernSleepRecord(childernSleepRecord, false);
        long id = teacherSleepRecord.getId();
        // 判断是否存在
        TeacherSleepRecord byId = teacherSleepRecordService.getById(id);
        ThrowUtils.throwIf(byId == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = teacherSleepRecordService.updateById(teacherSleepRecord);
        return ResultUtils.success(result);
    }
    @PostMapping("/list/page")
    @ApiOperation(value = "分页获取午睡记录")
    public BaseResponse<Page<TeacherSleepRecord>> listPostByPage(@RequestBody TeacherSleepRecordQueryRequest teacherSleepRecordQueryRequest) {
        long current = teacherSleepRecordQueryRequest.getCurrent();
        long size = teacherSleepRecordQueryRequest.getPageSize();
        QueryWrapper<TeacherSleepRecord> queryWrapper = teacherSleepRecordService.getQueryWrapper(teacherSleepRecordQueryRequest);
        Page<TeacherSleepRecord> page = teacherSleepRecordService.page(new Page<>(current, size),queryWrapper);
        return ResultUtils.success(page);
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出午睡记录")
    public BaseResponse<String> export(@RequestBody Long[] ids) throws IOException {
        return ResultUtils.success(teacherSleepRecordService.export(ids));

    }

    @PostMapping("/update/batch")
    @ApiOperation(value = "批量更新午睡记录")
    public BaseResponse<Boolean> updateBatch(@RequestBody List<TeacherSleepRecord> teacherSleepRecordList) {
        if (teacherSleepRecordList == null || teacherSleepRecordList.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新的午睡记录不能为空");
        }
        for (TeacherSleepRecord teacherSleepRecord : teacherSleepRecordList) {
            //查询记录是否存在
            TeacherSleepRecord byId = teacherSleepRecordService.getById(teacherSleepRecord.getId());
            ThrowUtils.throwIf(byId == null, ErrorCode.NOT_FOUND_ERROR, "记录不存在");

        }
        boolean result = teacherSleepRecordService.updateBatchById(teacherSleepRecordList);
        return ResultUtils.success(result);
    }
}
